package com.pinyougou.shop.realm;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.pojo.Seller;
import com.pinyougou.service.SellerService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class SellerAuthorizingRealm extends AuthorizingRealm
{
    @Reference(timeout=10000)
    private SellerService sellerService;

    /** 授权方法 */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection)
    {

        return null;
    }

    /** 认证方法 */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
            throws AuthenticationException
    {
        //获取用户名
        String sellerId = authenticationToken.getPrincipal().toString();
        //查找用户
        Seller seller = sellerService.findOne(sellerId);
        //非空和状态判断后
        if(seller != null && "1".equals(seller.getStatus()))
        {
            //返回认证信息
            return new SimpleAuthenticationInfo(sellerId,seller.getPassword(), ByteSource.Util.bytes(seller.getSellerId()),this.getName());
        }
        return null;
    }
}